package com.flame.file.controller;

import java.util.List;

import javax.servlet.http.HttpServletResponse;

import com.flame.core.protocol.PageResult;
import com.flame.core.protocol.Result;
import com.flame.core.utils.HttpUtils;
import com.flame.file.controller.vo.StaticFileInfoVo;
import com.flame.file.entity.FileInfo;
import com.flame.file.entity.StaticFileInfo;
import com.flame.file.exception.FileInfoExceptionMessage;
import com.flame.file.exception.FileInfoRuntimeException;
import com.flame.file.service.StaticFileInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

/**
 * flame_sys_static_file_info 系统静态资源表
 *
 * @author 吴欣童
 * @since 2023-03-16 11:00:18
 */
@RestController
public class StaticFileInfoController {

    @Autowired
    private StaticFileInfoService staticFileInfoService;

    @GetMapping("/static/file")
    public void download(@RequestParam("fileId") Long fileId, HttpServletResponse response) {
        StaticFileInfo dbFileInfo = staticFileInfoService.getFileInfo(fileId);
        if (dbFileInfo == null) {
            throw new FileInfoRuntimeException(FileInfoExceptionMessage.FILEINFO_NOT_EXIST);
        }
        String fileFullName = dbFileInfo.getFileName();
        if (StringUtils.hasText(dbFileInfo.getFileType())) {
            fileFullName = fileFullName + "." + dbFileInfo.getFileType();
        }
        HttpUtils.writeFile(response, fileFullName, dbFileInfo.getFileLocation(), false);
    }

    @PostMapping("/static/file/page")

    public PageResult pageStaticFileInfo(@RequestBody StaticFileInfoVo staticFileInfoVo) {
        return PageResult.success(staticFileInfoService.pageStaticFileInfo(staticFileInfoVo));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/static/file/upload")
    public Result addStaticFileInfo(@RequestParam("file") MultipartFile file) {
        return Result.success(staticFileInfoService.save(file));
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/static/file/url")
    public Result getFileUrl(@RequestBody @Validated StaticFileInfo staticFileInfo) {
        return Result.success("/static/file?fileId=" + staticFileInfo.getFileId());
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/static/file/delete")
    public Result deleteStaticFileInfo(@RequestBody @Validated StaticFileInfo staticFileInfo) {
        staticFileInfoService.deleteStaticFileInfo(staticFileInfo.getFileId());
        return Result.success();
    }

    @Transactional(rollbackFor = Exception.class)
    @PostMapping("/static/file/batch/delete")
    public Result batchDeleteStaticFileInfo(@RequestBody @Validated List<Long> fileIdList) {
        for (Long fileId : fileIdList) {
            staticFileInfoService.deleteStaticFileInfo(fileId);
        }
        return Result.success();
    }
}
